package com.flyee.controller.tools.cartoon.control
{
	import com.flyee.controller.tools.cartoon.model.ConfigObj;
	import com.flyee.controller.tools.cartoon.model.CreateAppearanceModel;
	import com.flyee.controller.tools.cartoon.model.EquipObj;
	import com.flyee.controller.tools.cartoon.model.EquipXMLHelper;
	import com.flyee.controller.tools.cartoon.model.EquipsObj;
	import com.flyee.controller.tools.cartoon.view.CartoonSprite;
	import com.flyee.events.AppearanceEvent;
	import com.flyee.utils.MyLog;
	import com.flyee.utils.textload.TextLoader;
	
	import fl.motion.Animator;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	
	public class AppearanceInfoManager
	{
		private var myEquipXML:EquipXMLHelper = null;
		private var changeColorArray:Array = null;
		private var myID:String = null;
		//各个零件容器数组
		private var partSpriteArray:Array = new Array();
		//每个动作（由各个零件组成）的容器数组
		private var spriteManagerArray:Array = new Array();
		//截取好图片的数组
		private var appearanceArray:Array = new Array();
		//用于判断是否截取完毕的数组
		private var spriteManagerArray2:Array = null;
		
		private var timer:Timer = new Timer(100);
		
		private var needCutArray:Array = null;
		
		private var cutFrame:uint = 0;
		public function AppearanceInfoManager()
		{
		}
		public function getInfo():EquipXMLHelper
		{
			return myEquipXML;
		}
		/**
		 *获取截图数组 
		 * @return 
		 * 
		 */		
		public function getAppearance():Array
		{
			return this.appearanceArray;
		}
		/**
		 *获取零件容器数组 
		 * @return 
		 * 
		 */		
		public function getPartArray():Array
		{
			return this.partSpriteArray;
		}
		/**
		 *获取每个动作容器数组 
		 * @return 
		 * 
		 */		
		public function getSpriteManagerArray():Array
		{
			return this.spriteManagerArray;
		}
		public function setInfo(model:CreateAppearanceModel):void
		{
			if(model == null)
			{
				return;
			}
			myID = model.appearanceCartoon.getID();
			if(model.queipXML == null)
			{
				var loader:TextLoader = new TextLoader(ConfigObj.absoluteURL+ConfigObj.defaultConfigURL+"equipconfig.xml",this.getInitConfig)
				loader.load();
			}
			else
			{
				getInitConfig(model.queipXML.toString())
			}
		}
		private function getInitConfig(data:String):void
		{
			var temp:XML = new XML(data);
			this.myEquipXML = new EquipXMLHelper(temp,myID);
			this.myEquipXML.addEventListener("createComplete",completeHandle);
			//this.init();
		}
		private function completeHandle(e:Event):void
		{
			needCutArray = new Array();
			cutFrame = 1;
			this.drawImage();
			
		}
		public function startCut():void
		{
			needCutArray = new Array();
			cutFrame = 1;
			timer.addEventListener(TimerEvent.TIMER,timerHandle);
			timer.start();
		}
		private function drawImage():void
		{
			timer.addEventListener(TimerEvent.TIMER,timerHandle);
			timer.start();
		}
		private function timerHandle(e:TimerEvent):void
		{
			var tempArray:Array = this.myEquipXML.getEquipList();
			var tempLength:uint = tempArray.length
			for(var i:int = 0;i< tempLength;i++)
			{
				var tempEquips:EquipsObj = tempArray[i];
				if(tempEquips)
				{
					var tempArray2:Array = tempEquips.getEquipList();	
					var tempLength2:uint = tempArray2.length;
					for(var j:int = 0; j< tempLength2;j++)
					{
						var tempEquip:EquipObj = tempArray2[j];
						var tempAnimator:Animator = tempEquip.myAnimator;
						
						if(!tempAnimator || this.cutFrame == 1)
						{
							
						}
						else if(tempAnimator)
						{
							if(j == 0)
							{
								//每个SPRITE 每个TIMER 只截取1次图像
								this.getBitmap(tempEquips.mySprite,tempAnimator.time - 1);
							}
							if(tempAnimator.motion.duration <= tempAnimator.time +1)
							{
								//此时该方向的所有帧都被截取，释放该方向占用的所有资源
								var tempAppCartoon:CreateAppearanceModel = AppearanceMananger.getInstance().getAppCartoon(this.myID);
								tempAppCartoon.appearanceCartoon.dispath(new AppearanceEvent(AppearanceEvent.COMPLETE_APPEARANCE,{dir:tempEquips.direction,type:tempEquips.type}))
								this.clearTagSpriteManager(tempEquips.direction,tempEquips.type)
								break;
							}
						}
						else
						{
							MyLog.debug("eorro by cartoon")
						}
						tempAnimator.time = this.cutFrame%tempAnimator.motion.duration;
					}
				}
			}
			this.cutFrame ++;
			isSpriteManagerALLClear();
		}
		private function getBitmap(t:CartoonSprite,index:uint):void
		{
			t.x = 0;
			t.y = 0;
			var tempRe:Rectangle = t.transform.pixelBounds;
			var bitmapdata:BitmapData = new BitmapData(150,200,true,0);
			var tempSprite:Sprite = new Sprite();
			tempSprite.addChild(t);
			t.x = 20;
			//t.y = -tempRe.y;
			bitmapdata.draw(tempSprite,null,null,null,new Rectangle(0,0,150,200));
			if(this.appearanceArray[t.myType] == null)
			{
				this.appearanceArray[t.myType] = new Array();
			}
			if(this.appearanceArray[t.myType][t.myDir] == null)
			{
				this.appearanceArray[t.myType][t.myDir] = new Array();
			}
			var tempBitmap:Bitmap = new Bitmap(bitmapdata);
			tempBitmap.x = -20;
			this.appearanceArray[t.myType][t.myDir][index] = tempBitmap;

		}
		private function clearTagSpriteManager(dir:uint,type:uint):void
		{
			var temp:EquipsObj = this.myEquipXML.getEquips(type,dir)
			if(temp)
			{
				var tempLength:uint = this.needCutArray.length;
				for(var i:int = 0; i< tempLength; i++)
				{
					if(this.needCutArray[i] == temp)
					{
						return;
					}
				}
				this.needCutArray.push(temp);
			}
		}
		private function isSpriteManagerALLClear():Boolean
		{
			var tempLength:uint = this.needCutArray.length;
			if(tempLength < this.myEquipXML.getEquipList().length)
			{
				return false;
			}
			//此时所有图片截取完成 广播完成事件 清空该对象占用的所有资源
			this.needCutArray.splice(0);
			AppearanceMananger.getInstance().setAllAppearance(this.myID,this.appearanceArray);
			var tempAppCartoon:CreateAppearanceModel = AppearanceMananger.getInstance().getAppCartoon(this.myID);
			tempAppCartoon.appearanceCartoon.dispath(new AppearanceEvent(AppearanceEvent.ALL_COMPLETE_APPEARANCE))
			MyLog.debug(tempAppCartoon.appearanceCartoon == ChangeEquip.main.myCartoon)
			if(timer)
			{
				timer.removeEventListener(TimerEvent.TIMER,timerHandle);
				timer.stop();
			}
			return true;
		}
		public function clear():void
		{
			if(timer)
			{
				timer.removeEventListener(TimerEvent.TIMER,timerHandle);
				timer.stop();
				timer = null;
			}
			if(myEquipXML)
			{
				myEquipXML.clear();
				myEquipXML = null;
			}
			if(this.needCutArray)
			{
				this.needCutArray.splice(0);
				this.needCutArray = null;
			}
			if(this.spriteManagerArray)
			{
				var tempLength:uint = this.spriteManagerArray.length;
				for(var i:uint = 0; i < tempLength; i++)
				{
					if(this.spriteManagerArray[i] != null)
					{
						var temp:CartoonSpriteManager = this.spriteManagerArray[i];
						if(temp)
						{
							temp.clear();
						}
						this.spriteManagerArray[i] = null;
					}
				}
				this.spriteManagerArray.splice(0);
				this.spriteManagerArray = null;
			}
			if(this.appearanceArray)
			{
				this.appearanceArray.splice(0);
				this.appearanceArray = null;
			}
		}
	}
}